﻿Imports System.Security.Principal
Imports Microsoft.Win32
'Creator - HF Tweety
'Profile - http://www.hackforums.net/member.php?action=profile&uid=1234585
'Website - www.codeineject.co.uk
'Twitter - @Codeinject
''' <summary>
''' Net Script is a light weight application to run text files as .NET application or scripts
''' It transforms the language into a scripting language, making it as powerful and easy to use as Batch files or VB 
''' </summary>
''' <remarks>
''' </remarks>
Module Install
    Public Path As [String] = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\Net Script\"
    Sub Main()
        Console.Title = "Net Script Core Installer"
        Console.WriteLine(My.Resources.Header)
        Console.WriteLine("Written by @Codeinject")
        Console.WriteLine("HF Tweety")
        Console.WriteLine(Environment.NewLine)
        Console.WriteLine(String.Format("Root Installation path: {0}", Path))
        Console.WriteLine(String.Format("Administrator Account: {0}", Adminstrator))
        Call AdminText()
        Call Install()
        Call Register()
        Console.ReadLine()
        Console.ReadLine()
        Console.ReadLine()

    End Sub
    Sub CheckInstall()
        If IO.Directory.Exists(Path + "Net Script Core.exe") Then
            Console.WriteLine("Press enter to uninstall Net Script")
            Console.Read()
            IO.Directory.Delete(Path, True)
            Unregister()
            Console.WriteLine("Uninstalled!")
            Console.Read()
            Process.GetCurrentProcess.Kill()
            End
        End If
    End Sub
    Sub Register()
        Console.WriteLine("Registering and creating keys...")

        Registry.ClassesRoot.CreateSubKey(".csc").SetValue("", "CSharpService", Microsoft.Win32.RegistryValueKind.String)
        ' Console.ReadKey()
        Registry.ClassesRoot.CreateSubKey("CSharpService\shell\open\command").SetValue("", ChrW(34) & Path + "Net Script Core.exe" & ChrW(34) & " ""%1"" ", Microsoft.Win32.RegistryValueKind.String)
        ' Console.ReadKey()

        Registry.ClassesRoot.CreateSubKey(".vbc").SetValue("", "VBService", Microsoft.Win32.RegistryValueKind.String)
        ' Console.ReadKey()
        Registry.ClassesRoot.CreateSubKey("VBService\shell\open\command").SetValue("", ChrW(34) & Path + "Net Script Core.exe" & ChrW(34) & " ""%1"" ", Microsoft.Win32.RegistryValueKind.String)
            Console.WriteLine("installed successfully!!")
            Console.Read()
    End Sub
    Sub Unregister()
        Try
            Console.WriteLine("Destroying keys...")
            My.Computer.Registry.ClassesRoot.DeleteSubKey(".vbc")
            My.Computer.Registry.ClassesRoot.DeleteSubKey("VBService")
            My.Computer.Registry.ClassesRoot.DeleteSubKey(".csc")
            My.Computer.Registry.ClassesRoot.DeleteSubKey("CSharpService")
            Catch
        End Try
    End Sub
    Sub Install()
        CheckInstall()
        Console.WriteLine("Press enter to install Net Script")
        Console.Read()
        If Not IO.Directory.Exists(Path) Then IO.Directory.CreateDirectory(Path)
        Console.WriteLine("Creating directory...")
        IO.File.WriteAllBytes(Path + "vbc.exe", My.Resources.vbc)
        Console.WriteLine("Installed vbc.exe...")
        IO.File.WriteAllBytes(Path + "csc.exe", My.Resources.csc)
        Console.WriteLine("Installed csc.exe...")
        IO.File.WriteAllBytes(Path + "Net Script Core.exe", My.Resources.Net_Script_Core)
        Console.WriteLine("Installed Net Script Core.exe...")
    End Sub
    Sub AdminText()
        If Not Adminstrator() Then
            Console.WriteLine("You must be running this installation package as admin to continue")
            Console.Read()
            Process.GetCurrentProcess.Kill()
        End If
    End Sub
    Private Function Adminstrator() As Boolean
        Dim identity = WindowsIdentity.GetCurrent()
        Dim principal = New WindowsPrincipal(identity)
        Dim isElevated As Boolean = principal.IsInRole(WindowsBuiltInRole.Administrator)
        If Not isElevated Then
            Return False
        End If
        Return True
    End Function
End Module
